// let morefunBase = "https://starbucksc2m.morefun.me";
// let morefunBase = "http://beastworse.tpddns.cn:9081";
// let morefunBaseLamer = "http://116.198.44.39"; // lamer
let morefunBase = "http://39.107.24.112:9090/prod-api"; // 党超的
let morefunBaseNew = "http://beastworse.tpddns.cn:9082";// 龙刚的

let token = "";

// 登录？
function querylogin() {
        return `${morefunBase}/applet/login`
}

// 获取菜单数据
function getMenuData() {
        return `${morefunBase}/applet/meuedata`
}

// 获取用户的openid
function getUserByJdCode() {
        return `${morefunBaseNew}/getOpenIdByCode`
}

// 埋点
function addUserStat() {
        return `${morefunBaseNew}/addUserStat`
}

// 上传图片到oss
function uploadFileUrl(inBizPath = "customUploadFiles") {
        return `${morefunBaseNew}/uploadFile/${inBizPath}`
        // return `${morefunBaseFile}/cupHome/uploadCustomFile`
}

function updateToken(inT) {
        token = inT;
}

// jd.uploadFile({
//         url: mfApi.uploadCustomFile(), //仅为示例，非真实的接口地址
//         filePath: tempFilePaths[0],
//         name: 'customFile',
//         formData: {
//                 'user': 'test'
//         },
//         success(res) {
//                 console.log(res)
//                 const data = res.data
//                 //do something
//         }
// })

function uploadFile(inUrl, inLocalFilePath) {
        return new Promise(function (resolve, reject) {
                jd.uploadFile({
                        url: inUrl,
                        filePath: inLocalFilePath,
                        name: 'file',
                        success(res) {
                                console.log("api --- http --- res ")
                                console.log(res)
                                if (res.statusCode == "200") {
                                        resolve(JSON.parse(res.data))
                                } else {
                                        jd.hideLoading();
                                        jd.showToast({
                                                title: '请检查网络连接。',
                                                icon: 'none'
                                        })
                                        reject(res);
                                }
                        },
                        fail: err => {
                                jd.hideLoading();
                                jd.showToast({
                                        title: '请检查网络连接。',
                                        icon: 'none'
                                })
                                reject(err);
                        }
                })
        })
}


function http(inUrl, inMethod, inData) {
        return new Promise(function (resolve, reject) {
                jd.request({
                        url: inUrl, //仅为示例，并非真实的接口地址
                        method: inMethod,
                        data: inData,
                        header: {
                                'content-type': 'application/json',
                                'Authorization': 'Bearer ' + token
                        },

                        // 'Authorization'] = 'Bearer ' + token;

                        success: res => {
                                // console.log("api --- http --- res ",res)
                                if (res.statusCode == "200") {
                                        resolve(res.data)
                                } else {
                                        jd.hideLoading();
                                        jd.showToast({
                                                title: '请检查网络连接。',
                                                icon: 'none'
                                        })
                                        reject(res);
                                }
                        },
                        fail: err => {
                                jd.hideLoading();
                                jd.showToast({
                                        title: '请检查网络连接。',
                                        icon: 'none'
                                })
                                reject(err);
                        }
                })
        });
}

module.exports = {
        addUserStat,
        getUserByJdCode,
        uploadFileUrl,

        querylogin,
        getMenuData,

        http: http,
        uploadFile: uploadFile,
        updateToken: updateToken,
};

// module.exports = {
//         formatTime: formatTime
// };